﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NRemedy;
using BMC.ARSystem;
using Wicresoft.UnifyShow2.Portal.Models;
using System.Configuration;

namespace Wicresoft.UnifyShow2.Portal.Service
{
    public class MsgHelper
    {
        private static readonly string ARServer = ConfigurationManager.AppSettings["ARServer"];
        private static readonly string AdminName = ConfigurationManager.AppSettings["ARUid"];
        private static readonly string AdminPassword = ConfigurationManager.AppSettings["ARPwd"];

        public bool Create(WS_Message model)
        {
            bool ret = false;
            if (string.IsNullOrEmpty(model.cChr_LoginID))
                return false;
            var context = new ARLoginContext(ConfigurationManager.AppSettings["ARServer"],
                ConfigurationManager.AppSettings["ARUid"],
                ConfigurationManager.AppSettings["ARPwd"]);
            try
            {
                context.ServerInstance.CreateEntry("WS_Message", new ModelBinder<WS_Message>().UnBindForUpdate(model));
                ret = true;
            }
            catch
            {
                ret = false;
            }
            finally
            {
                context.Dispose();
            }
            return ret;

        }

        public void CreateMessage(WS_Message model) 
        {
            try 
            {
                using (var arContext = new ARLoginContext(ARServer, AdminName, AdminPassword)) 
                {
                    ARProxy<WS_Message> ar = new ARProxy<WS_Message>(arContext);
                    ar.CreateEntry(model);
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }

    }
}